package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.FamilySubjectComponent;
import com.go.where.family.constants.Constant;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.FamilySubjectInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.FamilySubjectService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.FamilySubjectVo;
import com.google.gson.Gson;

@Component("familySubjectComponent")
public class FamilySubjectComponentImpl implements FamilySubjectComponent {

	private static final Logger log = Logger.getLogger(FamilySubjectComponentImpl.class);
	private Gson gson;
	@Resource(name = "familySubjectService")
	private FamilySubjectService familySubjectService;

	public FamilySubjectComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<FamilySubjectInfo> queryList(String body, Head head) {
		FamilySubjectVo vo = gson.fromJson(body, FamilySubjectVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("isDelete", Constant.IS_NOT_DELETE);
			map.put("month", vo.getMonth());
			map.put("isHot", vo.getIsHot());
			map.put("today", vo.getToday());
			if (StringUtils.isNotBlank(vo.getOrderBy())) {
				if (Constant.ORDER_TYPE_ASC.equals(vo.getOrderType())) {
					map.put("page_orderByAsc", vo.getOrderBy());
				} else {
					map.put("page_orderByDesc", vo.getOrderBy());
				}
			}

			List<FamilySubjectInfo> list = familySubjectService.queryList(map);
			if (list != null && !list.isEmpty()) {
				return list;
			} else {
				return new ArrayList<FamilySubjectInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取话题列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public FamilySubjectInfo querySubjectDetail(String body, Head head) {
		FamilySubjectVo vo = gson.fromJson(body, FamilySubjectVo.class);
		if (vo != null) {
			String subId = vo.getSubjectId();
			FamilySubjectInfo subject = familySubjectService.querySubjectDetail(subId);
			if (subject != null) {
				return subject;
			} else {
				log.info("requestId:" + head.getRequestId() + ",查询话题详情信息时,没有查询到数据,话题ID：" + subId);
				throw new ServerDisponseException(TransmissionCode.NO_DATA.getCode(),
						TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取话题详情信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean updateSupport(String body, Head head) {
		FamilySubjectVo vo = gson.fromJson(body, FamilySubjectVo.class);
		if (vo != null) {
			String subId = vo.getSubjectId();
			FamilySubjectInfo subject = familySubjectService.querySubjectDetail(subId);
			if (subject != null) {
				Integer rightSupport = subject.getRightSupport();
				Integer oppositionSupport = subject.getOppositionSupport();
				if ("1".equals(vo.getRightSupportOrNot())) {
					rightSupport = CommonUtil.add(rightSupport, 1);
				} else if ("0".equals(vo.getRightSupportOrNot())) {
					rightSupport = CommonUtil.sub(rightSupport, 1);
				}
				if ("1".equals(vo.getOppositionSupportOrNot())) {
					oppositionSupport = CommonUtil.add(oppositionSupport, 1);
				} else if ("0".equals(vo.getOppositionSupportOrNot())) {
					oppositionSupport = CommonUtil.sub(oppositionSupport, 1);
				}
				FamilySubjectInfo sub = new FamilySubjectInfo();
				sub.setSubjectId(subId);
				sub.setRightSupport(rightSupport);
				sub.setOppositionSupport(oppositionSupport);
				int re = familySubjectService.update(sub);
				if (re > 0) {
					return true;
				} else {
					return false;
				}
			} else {
				log.info("requestId:" + head.getRequestId() + ",更新话题支持人数时,没有查询到数据,话题ID：" + subId);
				throw new ServerDisponseException(TransmissionCode.NO_DATA.getCode(),
						TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",更新话题支持人数时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}
}
